import java.awt.Point;

public class House implements Buildings {

	private byte buildLevel;
	//done when update freq has been set
	private float timeMultiplier = 1f;

	public House() {
		this((byte) 0);
	}

	public House(byte level) {
		this.buildLevel = level;
	}

	// the server tells house to add residents at constant rate, level
	// determines how fast.
	public float growth(float curRes) {
		switch (buildLevel) {
		case 0:
			if (curRes <= 20f)
				return 0.198f * timeMultiplier;
			return 0f;
		case 1:
			if (curRes >= 40f)
				return 0.296f * timeMultiplier;
			return 0f;
		case 2:
			if (curRes >= 60f)
				return 0.444f * timeMultiplier;
			return 0f;
		case 3:
			if (curRes >= 80f)
				return 0.667f * timeMultiplier;
			return 0f;
		case 4:
			if (curRes >= 100f * timeMultiplier)
				return 1f; // adds one resident every update interval.
			return 0f;
		default:
			return 0f;
		}
	}

	/**
	 * Upgrade house from one level to the next, and removes residents
	 * accordingly
	 * 
	 * @return time until done
	 */
	public int upgradeTime() {
		switch (buildLevel) {
		case 0:
			return 2000;
		case 1:
			return 5000;
		case 2:
			return 10000;
		case 3:
			return 10000;
		default:
			return 0;
		}
	}

	private int upgradeCost() {
		switch (buildLevel) {
		case 0:
			return 5;
		case 1:
			return 10;
		case 2:
			return 20;
		case 3:
			return 999;
		default:
			return 0;
		}
	}

	public boolean isUpgradeable(float res) {
		return (buildLevel < 4) && res > upgradeCost();
	}

	/**
	 * Outside call that building is completed.
	 */
	public void upgradeDone() {
		this.buildLevel++;
	}

	/**
	 * 
	 * @param attackMorale
	 *            attackers morale (0-5)
	 * @param ownMorale
	 *            own morale (0-5)
	 * @param attackArmory
	 *            attackers number of armory's
	 * @return true if successful defense
	 */
	public float defend(byte attackMorale, byte ownMorale, byte attackArmory) {
		// some formula to reduce number of residents.
		float reduce = (((float) (attackMorale - ownMorale)) / 5f)
				+ ((float) (attackArmory - buildLevel) / 4f);
		if (reduce < 0) {
			reduce = Math.abs(1 / (reduce - 1));
		} else {
			reduce++;
		}
		return reduce;
	}

	public void setBuildLevel(byte n) {
		buildLevel = n;
	}

	public byte getBuildLevel() {
		return buildLevel;
	}

	public House clone() {
		return new House(buildLevel);
	}

	public House clone(byte level) {
		return new House(level);
	}

	public void InRange(Point pos, Unit u) {
		return;
	}
}